import Parser from 'rss-parser';
import fs from 'fs';
import path from 'path';

// The Verge 使用 Atom feed，rss-parser 会自动处理
const parser = new Parser();
const rssUrl = 'https://www.theverge.com/rss/index.xml';

// 解码 HTML 实体
function decodeHtmlEntities(text) {
  if (!text) return text;
  return text
    .replace(/&#8217;/g, "'")
    .replace(/&#8211;/g, "–")
    .replace(/&#8212;/g, "—")
    .replace(/&#160;/g, " ")
    .replace(/&#8230;/g, "…")
    .replace(/&#038;/g, "&")
    .replace(/&amp;/g, "&")
    .replace(/&nbsp;/g, " ")
    .replace(/&lt;/g, "<")
    .replace(/&gt;/g, ">")
    .replace(/&quot;/g, '"')
    .replace(/&#39;/g, "'");
}

// 从 HTML content 中提取第一张图片 URL
function extractImageUrl(content) {
  if (!content) return null;
  const imgMatch = content.match(/<img[^>]+src=["']([^"']+)["']/i);
  if (imgMatch && imgMatch[1]) {
    // 解码 HTML 实体
    return decodeHtmlEntities(imgMatch[1]);
  }
  return null;
}

async function testRSS() {
  try {
    console.log('正在获取 RSS 订阅...');
    const feed = await parser.parseURL(rssUrl);
    
    console.log('\n=== RSS 订阅信息 ===');
    console.log('标题:', feed.title);
    console.log('链接:', feed.link);
    console.log('最后更新时间:', feed.lastBuildDate || feed.pubDate);
    console.log(`\n总共获取到 ${feed.items.length} 篇文章\n`);
    
    // 显示所有文章
    console.log('=== 所有文章列表 ===\n');
    feed.items.forEach((item, index) => {
      console.log(`${index + 1}. ${item.title}`);
      console.log(`   链接: ${item.link}`);
      console.log(`   发布时间: ${item.pubDate || '未知'}`);
      if (item.author) {
        console.log(`   作者: ${item.author}`);
      }
      const description = item.summary || item.contentSnippet || '';
      if (description) {
        // 移除 HTML 标签用于显示
        const textOnly = description.replace(/<[^>]*>/g, '').replace(/&[^;]+;/g, ' ');
        console.log(`   摘要: ${textOnly.substring(0, 150)}${textOnly.length > 150 ? '...' : ''}`);
      }
      // 显示图片
      const imageUrl = extractImageUrl(item.content) || extractImageUrl(item.summary);
      if (imageUrl) {
        console.log(`   图片: ${imageUrl}`);
      }
      console.log('');
    });
    
    // 保存完整数据到 JSON 文件
    const outputPath = path.join(process.cwd(), 'test', 'theverge-rss-data.json');
    const outputData = {
      feed: {
        title: feed.title,
        link: feed.link,
        lastBuildDate: feed.lastBuildDate || feed.pubDate,
        itemCount: feed.items.length
      },
      items: feed.items.map(item => ({
        title: decodeHtmlEntities(item.title),
        link: item.link,
        pubDate: item.pubDate,
        author: item.author || null,
        description: decodeHtmlEntities(item.summary || item.contentSnippet || null),
        content: item.content || null,
        guid: item.id || item.link,
        imageUrl: extractImageUrl(item.content) || extractImageUrl(item.summary) || null
      }))
    };
    
    fs.writeFileSync(outputPath, JSON.stringify(outputData, null, 2), 'utf-8');
    console.log(`\n✅ 完整数据已保存到: ${outputPath}`);
    
  } catch (error) {
    console.error('获取 RSS 失败:', error.message);
    if (error.code === 'ENOTFOUND') {
      console.error('网络连接失败，请检查网络');
    } else if (error.code === 'ECONNREFUSED') {
      console.error('连接被拒绝，请检查 URL 是否正确');
    }
  }
}

testRSS();

